package com.xywl.escort.adapter;

import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.xywl.escort.R;
import com.xywl.escort.bean.SpecificResp;

import java.util.HashMap;
import java.util.List;

import timber.log.Timber;

public class SpecificAdapter extends BaseQuickAdapter<SpecificResp, BaseViewHolder> {
    public SpecificAdapter(int layoutResId, @Nullable List<SpecificResp> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, SpecificResp item) {
        RecyclerView recyclerSpec = helper.getView(R.id.recycler_spec);
        GridLayoutManager manager = new GridLayoutManager(mContext, 4);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerSpec.setLayoutManager(manager);

        final List<SpecificResp.Specific> list = item.data;
        SpecificListAdapter adapter = new SpecificListAdapter(R.layout.item_specific_view, list);
        recyclerSpec.setAdapter(adapter);
        final HashMap<String, SpecificResp.Specific> map = item.map;
        final String key = item.id;

        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                SpecificResp.Specific specific = (SpecificResp.Specific) adapter.getData().get(position);
                Timber.tag("SpecificAdapter").e("specific=" + specific);
                map.put(key, specific);

                String name = specific.value;
                for (int i = 0; i < list.size(); i++) {
                    list.get(i).isClick = list.get(i).value.equals(name);
                }
                adapter.notifyDataSetChanged();
            }
        });
    }
}
